package cn.com.callback

import cn.com.kh.KHDBConfig
import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import org.json.JSONException;

/**
 *
 * @Title: api_Get_WYSGToKen
 * @Description: 深水 宝安外业三高 获取ToKen
 * @author: chieh
 * @date 2022-01-19 10:30:00
 *
 */
public class api_Get_WYSGToKen implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(api_Get_WYSGToKen.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        log.debug("-------------------------start api_Get_WYSGToKen 开始-----------------");

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        //获取配置信息
        String URL = KHDBConfig.getAPIURL(eliteBean, "BAWY_WSWorkFlow") + "/api/wap/v2/auth/logon";
        String account = KHDBConfig.getParamConfig(eliteBean, "BAWY_WSWorkAPI_ID");
        String password = KHDBConfig.getParamConfig(eliteBean, "BAWY_WSWorkAPI_PWD");
        String appIdentity = "ws";
        String clientKey = "1";

        long startTime = System.currentTimeMillis();

        String reJson = "";
        try {

            String json = "{\"password\":\"" + password + "\",\"appIdentity\":\"" + appIdentity + "\",\"account\":\"" + account + "\",\"clientKey\":\"" + clientKey + "\"}";
            log.debug("宝安外业-ToKen调用请求报文：" + json);

            Map headMap = new HashMap();
            headMap.put("Content-Type", "application/json");

            String getResult = Util.doHttpPostUseHead(URL, json, "utf-8", 100000, 0, headMap);
            log.debug("宝安外业-ToKen调用结果返回：" + getResult.toString());

            String token = null;
            def expiresIn = 0;
            if (getResult != null && getResult.length() > 0) {
                try {
                    JSONObject loginObject = new JSONObject(getResult);
                    String code = GetJV(loginObject, "code");
                    String data = GetJV(loginObject, "data");
                    log.debug("宝安外业-ToKen调用code:" + code);
                    log.debug("宝安外业-ToKen调用data:" + data);
                    if (code == "0") {
                        JSONObject dataObject = new JSONObject(data);
                        token = GetJV(dataObject, "token");
                        expiresIn = GetJV(dataObject, "expiresIn");
                        log.debug("宝安外业-ToKen调用token:" + token);
                        log.debug("宝安外业-ToKen调用expiresIn:" + expiresIn);

                        eliteBean.update("update SYSC_PARAMETER set PARAMETERVALUE = '" + token + "',MODIFIEDDATE=DATEADD(s," + expiresIn + ",GETDATE()) where PARAMETER_ID = 'BAWYSG'", "dataSource");
                        long endTime = System.currentTimeMillis();
                        reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"宝安外业获取ToKen接口调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":\"" + token + "\"}";
                    } else {
                        String message = GetJV(loginObject, "message");
                        long endTime = System.currentTimeMillis();
                        reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"宝安外业获取ToKen接口失败(" + message + ")，调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":\"" + token + "\"}";
                    }

                } catch (Exception e) {
                    token = null;
                    long endTime = System.currentTimeMillis();
                    reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"宝安外业获取ToKen接口失败(" + e.getMessage() + ")，调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":\"" + token + "\"}";
                }
            }


        } catch (JSONException e) {
            long endTime = System.currentTimeMillis();
            reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"宝安外业获取ToKen接口处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":{}}";
        }

        return reJson;
    }

    // 解析Json值
    public static String GetJV(JSONObject inJson, String getKey) {
        String Rev_Value = null;
        try {
            Rev_Value = inJson.getString(getKey);
        } catch (Exception e) {
            try {
                Rev_Value = String.valueOf(inJson.getInt(getKey));
            } catch (Exception e1) {
                try {
                    Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
                } catch (Exception e2) {
                    try {
                        Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
                    } catch (Exception e3) {
                        Rev_Value = "";
                    }
                }
            }
        } finally {
            if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
                Rev_Value = "";
            } else {
            }
        }
        return Rev_Value;
    }
}